package com.smartwebx.ha.biz.rainwater.real.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.smartwebx.ha.biz.rainwater.real.RsvrfsrManager;
import com.smartwebx.ha.dal.realtime.dao.RsvrfsrMapper;
import com.smartwebx.ha.dal.usebean.DateValue;
import com.smartwebx.ha.dal.usebean.RsvrfsrBean;
import com.smartwebx.ha.dal.util.DateFullProcess;

@Service
public class RsvrfsrManagerImpl implements RsvrfsrManager {

	@Autowired
	private RsvrfsrMapper rsvrfsrDao;

	@Override
	public List<DateValue> getSingleFloodProcess(Date startDate, Date endDate,
			String stcd) {
		List<RsvrfsrBean> rsvrfsrList = rsvrfsrDao.getRsvrfsrByStcd(stcd);
		Map<Date, Double> timePointMap = DateFullProcess.returnDateSectStr(startDate,
				endDate);

		List<DateValue> dayDvs = Lists.newArrayList();

		for (Date time : timePointMap.keySet()) {
			Date nowTime = DateUtils.setYears(time, 1970);

			DateValue dv = new DateValue();
			dv.setTm(time);
			Double val = 0d;
			dayDvs.add(dv);

			for (RsvrfsrBean rsvrfsr : rsvrfsrList) {
				if (nowTime.compareTo(rsvrfsr.getBgmd()) >= 0
						&& nowTime.compareTo(rsvrfsr.getEdmd()) <= 0) {
					val = rsvrfsr.getFsltdz();
					break;
				}
			}

			dv.setVal(val);
		}
		return dayDvs;
	}

	@Override
	public List<DateValue> getSingleFloodHourProcess(Date startDate,
			Date endDate, String stcd) {
		List<DateValue> floodProcessDay = getSingleFloodProcess(startDate,
				endDate, stcd);
		startDate = DateUtils.setHours(startDate, 0);
		endDate = DateUtils.setHours(endDate, 23);

		List<DateValue> hourDvs = Lists.newArrayList();

		int i = 0;
		while (endDate.compareTo(startDate = DateUtils.addHours(startDate, i)) != 0) {
			DateValue hourDv = new DateValue();
			Double val = 0d;
			hourDv.setTm(startDate);
			hourDvs.add(hourDv);

			for (DateValue floodDayDv : floodProcessDay) {
				if (floodDayDv.getTm().compareTo(startDate) == 0) {
					val = floodDayDv.getVal();
					break;
				}
			}
			hourDv.setVal(val);
			i++;
		}
		
		return hourDvs;
	}

}
